package org.hughie.service;

import org.hughie.api.FundApisMapping;
import org.hughie.aspect.Monitor;
import org.hughie.http.IHttpInvoker;
import org.hughie.mapper.BatchJobMapper;
import org.hughie.model.FundSearchResult;
import org.hughie.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author hughie.cheng
 * @date 2024/3/8
 */
@Service
public class FundQueryService {

    @Autowired
    private IHttpInvoker invoker;

    @Autowired
    private BatchJobMapper mapper;

    public List<FundSearchResult> queryByKey(String key) {
        List<FundSearchResult> list = invoker.getList(FundApisMapping.FUND_SEARCH_LIST,
                URLEncoder.encode(key, StandardCharsets.UTF_8), TimeUtil.getCurrentMills());
        return list;
    }

    @Monitor
    public List<FundSearchResult> saveByKey(String key) {
        List<FundSearchResult> list = invoker.getList(FundApisMapping.FUND_SEARCH_LIST,
                URLEncoder.encode(key, StandardCharsets.UTF_8), TimeUtil.getCurrentMills());
        list.forEach(t -> mapper.addJob(t.getCODE(), t.getNAME()));
        return list;
    }
}
